<?php
//FACEBOOK SETTINGS
include_once 'facebook.php';
include_once 'library.php';
include_once 'lib_map.php';
include_once 'lib_place.php';
include_once 'lib_player.php';

//FACEBOOK CLASS
$facebook = new Facebook($api_key, $secret);
$facebook->require_frame();

$facebook_id = $facebook->require_login();

//CLASSES
$player = getPlayerData($facebook_id);

if ($player == null) {
    echo "insertNewPlayer: ".insertNewPlayer($facebook_id);
    $player = getPlayerData($facebook_id);

}

$nav = $_GET['nav'];
$action = $_GET['action'];
$status = $_GET['status'];

//navigation
if ($nav) {
    if ($player->stamina > 0) {   
        switch($nav) {
        case 'North': 
            $player->loc_x--;
            break;
        case 'South': 
            $player->loc_x++;
            break;
        case 'West': 
            $player->loc_y--;
            break;
        case 'East': 
            $player->loc_y++;
            break;
        }
        //remove comment
        //$player->stamina--;
    } else {
        $nav = "";
    }

// action    
} elseif($action) {
    switch($action) {
    case 'Search':
        break;
    case 'Fight':
        break;
    case 'Challenge':
        break;
    case 'BuyItem':
        break;
    case 'SellItem':
        break;
    case 'DropItem':
        break;
    }
    
// status    
} elseif($status) {
    switch($status){
    case 'DrinkEnergy':
        break;
    case 'DrinkMana':
        break;
    }
}

//get the new location details
$location = getPlayerLocation($player);

//finally, save the player data
updatePlayerData($player);


// == PAGE START ==
echo '<style type="text/css">  <!-- ';

include_once 'style.css';

echo '--> </style> ';
?>


<table width="750" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td class="topNavigation">LEGENDS OF HEROES II - WORLD OF CHAOS</td>
  </tr>
</table>

<table width="750" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td class="topLeft"></td>
    <td class="topMiddle"><br/><div id="ajax_player_data">
    [ Health: <? echo "$player->health/$player->health_max" ?> | 
    Stamina: <? echo "$player->stamina/$player->stamina_max"; ?> |
    Strength: <? echo "$player->strength"; ?> |
    Dexterity: <? echo "$player->dexterity"; ?> |
    Energy: <? echo "$player->energy"; ?> |
    Skill: <? echo "$player->skill"; ?> |
    Coins: <? echo "$player->coins"; ?> ]
    </div>
    </td>
    <td class="topRight"></td>
  </tr>
</table>

<table width="750" height="200" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td width="30" class="midLeft" ></td>
    <td width="690" class="midMain" >


<table width="100%" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td width="48%" valign="top">
    <div class="profile_box clearfix">
        <div  class="box_head clearfix " >
          <table border="0" class="head_table" width="100%" cellpadding="0" cellspacing="0">
            <tr>
              <td><h2>Hero Stats</h2></td>
              <td align="right"></td>
            </tr>
          </table>
        </div>
        <div id="box_handle" class ="handle_invisible"></div>
        <div class="box_subhead clearfix" >
          <div class="box_subtitle">Displaying current stats</div>
          <div class="box_actions">Help</div>
        </div>
        <div class="inside_the_box reviews no_padding clearfix " >
          <div>

        <table class="review_display" width="100%"> 
            <tr>
                <td colspan="2">
                </td>
            </tr>
            <tr>

            <td class="review_pic_column"><div class="review_pic"><fb:profile-pic uid="<?php echo $facebook_id; ?>" size="square" /></div></td>
                <td>
                    <table width="100%"> 
                        <tr> 
                            <td>
                                <div class="review_header"> <div class="review_header_title_line">
                                <div class="review_header_title"><fb:name uid="<?=$facebook_id;?>" useyou="false"/></div> </div> 
                                <div class="review_header_subtitle_line">Expr: <?=$player->experience;?></div></div>
                                <div class="review_body">Last Played: <fb:date t="<?=strtotime($player->last_play);?>" /> 
                                </div>
                            </td> 
                        </tr>
                    </table>
                </td> 
            </tr> 
        </table>

            
        </div>
    </div>
    </div>
    <hr/>
    <div class="profile_box clearfix">
        <div  class="box_head clearfix " >
          <table border="0" class="head_table" width="100%" cellpadding="0" cellspacing="0">
            <tr>
              <td><h2>Hero Inventory</h2></td>
              <td align="right"></td>
            </tr>
          </table>
        </div>
        <div id="box_handle" class ="handle_invisible"></div>
        <div class="box_subhead clearfix" >
          <div class="box_subtitle">Showing {{ equipments_count }} items</div>
          <div class="box_actions">Help</div>
        </div>
        <div class="inside_the_box reviews no_padding clearfix " >
          <div>

            <table class="review_display" width="100%">
                <tr>
                    <td class="review_pic_column">
                        <div class="review_pic"><img src="<? echo IMAGE_HOST_URL.'equipment.image';?>" alt="{{ equipment.name }}" class="" /></div>
                        {{ equipment.type }}
                    </td>
                    <td>
                        <table width="100%"> <tr><td>
                        <div class="review_header"> <div class="review_header_title_line">
                        <div class="review_header_title">{{ equipment->name }}</div> </div> 
                        <div class="review_header_subtitle_line">Level {{ equipment->level }}</div></div>
                        <div class="review_body">
                            Power: {{ equipment->power_min }}<br/>
                            Attack: {{ equipment->attack_min }}<br/>
                            Defense: {{ equipment->defense_min }}
                        
                        </div>
                        </td>
                        </tr>
                        </table>
                    </td>
                </tr>
            </table>

          </div>
        </div>
      </div>

    </td>

<!-- // -->
    
    <td width="2%"></td>
    <td width="50%" valign="top">

    <div class="profile_box clearfix">
        <div  class="box_head clearfix " >
          <table border="0" class="head_table" width="100%" cellpadding="0" cellspacing="0">
            <tr>
              <td><h2>Hero Map Travels</h2></td>
              <td align="right"></td>
            </tr>
          </table>
        </div>
        <div id="box_handle" class ="handle_invisible"></div>
        <div class="box_subhead clearfix" >
          <div class="box_subtitle">Click highlighted square on map to travel.</div>
          <div class="box_actions">Help</div>
        </div>
        <div class="inside_the_box reviews no_padding clearfix " >
          <div>
          <div id="ajax_map">
          <? echo drawMap($player); ?>
          </div>
          
          </div>
        </div>
      </div>
<hr/>

<div class="profile_box clearfix">
        <div  class="box_head clearfix " >
          <table border="0" class="head_table" width="100%" cellpadding="0" cellspacing="0">
            <tr>
              <td><h2>Hero Actions</h2></td>
              <td align="right"></td>
            </tr>
          </table>
        </div>
        <div id="box_handle" class ="handle_invisible"></div>
        <div class="box_subhead clearfix" >
          <div class="box_subtitle">Hero Location Details</div>
          <div class="box_actions">Help</div>
        </div>
        <div class="inside_the_box reviews no_padding clearfix " >
          <div>
          <div id="ajax_search">
<? 
if ($location) { 
    $locationNPC = getLocationNPC($player);
    if ($locationNPC) {
        //display NPC
        displayLocationNPC($locationNPC);
        //display NPC items in shop
        displayNPCItems($locationNPC);        
    } else {
        displayLocation($location);
    }
} else { 
    echo "<p>There is noting here...</p>";
} 
?>            
          </div>
            
          </div>
        </div>
      </div>
      
    </td>
  </tr>
</table>


</td>
    <td width="30" class="midRight"></td>
  </tr>
</table>

<table width="750" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td width="105" class="bottomLeft"></td>
    <td width="540" class="bottomMiddle"></td>
    <td width="105" class="bottomRight"></td>
  </tr>
</table>
<br/>



<small>Copyright 2009 - Vatche Demirjian</small>

<?php 
    $callbackurl='http://chaos.legendsofheroes.com/action_ajax.php'; //you can find post_example.php file down.
    $opponent_id = 999; 
?> 
<h2>Div where test links:</h2> 
<div id="ajax_player">[[Nothing happening in this player div yet.]]</div><br/> 
<div id="ajax_fight">[[Nothing happening in this fight div yet.]]</div><br/>  
<h3>Select <a href="#" onclick="do_ajax('ajax',<?=$opponent_id;?>);return false;">Fight opponent</a></h3>
<input type="button" onclick="do_ajax('ajax',<?=$opponent_id;?>);return false;" value="Fight" ><br/> 

<script type="text/javascript"> 
<!-- 
function do_ajax(div,value) { 

    var ajax = new Ajax(); 
    ajax.responseType = Ajax.JSON;
    ajax.requireLogin = true; 
    ajax.ondone = function(data) { 
        document.getElementById(div+'_player').setTextValue(data.player);
        document.getElementById(div+'_fight').setInnerXHTML(data.fight);
    } 
    var params={"action":"fight","value":value}; //add parameters as comma separated "param":value 
    ajax.post('<?=$callbackurl?>',params); //GET values sended with "val" and POST values sended with "params" 
} 
//--> 
</script>
